package com.micronote.provider;

import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * This class helps open, create, and upgrade the database file.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = "NotePadProvider";

    public static final String DATABASE_NAME = "note_pad.db";
    public static final int DATABASE_VERSION = 2;
    public static final String NOTES_TABLE_NAME = "notes";
    private static DatabaseHelper mInstance = null;

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS notes");
        onCreate(db);
    }

	@Override
	public synchronized SQLiteDatabase getReadableDatabase() {
		// TODO Auto-generated method stub
		return super.getReadableDatabase();
	}

	@Override
	public synchronized SQLiteDatabase getWritableDatabase() {
		// TODO Auto-generated method stub
		return super.getWritableDatabase();
	}
    
    public static DatabaseHelper getInstance(Context c)
    {
    	if (mInstance == null) {
    		mInstance = new DatabaseHelper(c);
    	}
    	
    	return mInstance;
    }
    
    static public String createTable(String name, Map<String, String> columns)
    {
      StringBuffer sql = new StringBuffer();
      sql.append("CREATE TABLE " + name + "(");
      int i=0;
      for (Object key : columns.keySet().toArray())
      {
    	  if (i++ > 0) 
    		  sql.append(",");

    	  sql.append(key + " " + (String)columns.get(key));
      }
      sql.append(")");
      
      return sql.toString();
    }
}